﻿using AutoMapper;
using MediatR;
using T.S.BaseData.Api.AppClinet.Commend.Goods;
using T.S.BaseData.Api.Dto.Goods;
using T.S.BaseData.Domain.Entity;
using T.S.BaseData.ErrorCode;
using T.S.BaseData.Infrastructure.Base;

namespace T.S.BaseData.Api.AppClinet.CommendHand.Goods
{
    /// <summary>
    /// 显示商品信息
    /// </summary>
    public class GoodsShowCommandHandle : IRequestHandler<GoodsShowCommand, ApiResult<List<ShowGoodsDto>>>
    {
        private readonly IBaseRepository<GoodsInfo> goodsRepo;
        private readonly ILogger<GoodsShowCommandHandle> logger;
        private readonly IMapper mapper;

        public GoodsShowCommandHandle(IBaseRepository<GoodsInfo> goodsRepo,
            ILogger<GoodsShowCommandHandle> logger,
            IMapper mapper)
        {
            this.goodsRepo = goodsRepo;
            this.logger = logger;
            this.mapper = mapper;
        }

        public Task<ApiResult<List<ShowGoodsDto>>> Handle(GoodsShowCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<ShowGoodsDto>> result = new ApiResult<List<ShowGoodsDto>>();

            var good = goodsRepo.GetAll().Where(x => x.GoodsStatus == false);

            var model = mapper.Map<List<ShowGoodsDto>>(good);

            result.Code = ApiEnums.Success;
            result.Meg = "显示商品成功";
            result.Data = model;

            return Task.FromResult(result);
        }
    }
}
